package org.janusproject.demos.acl.basic.agent;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.janusproject.acl.ACLAgent;
import org.janusproject.acl.ACLMessage;
import org.janusproject.acl.Performative;
import org.janusproject.kernel.address.AgentAddress;
import org.janusproject.kernel.status.Status;
import org.janusproject.kernel.status.StatusFactory;
import org.janusproject.kernel.util.sizediterator.SizedIterator;

public class ACLSender extends ACLAgent {

	private static final long serialVersionUID = -7078376398776599011L;
	
	private boolean sent = false;
	
	public Status live() {
		
		ACLMessage message = new ACLMessage("Hey! my name is " + getName(), Performative.PROPOSE);
		SizedIterator<AgentAddress> agents = getLocalAgents();
		
		if (!sent) {
			while (agents.hasNext()) {
				AgentAddress agent = agents.next();
				if (agent != getAddress()) {
					sendACLMessage(message, agent);
					Logger logger = Logger.getAnonymousLogger();

					logger.log(Level.INFO,"\n=> NOUVEAU MESSAGE envoye par " + getName() + " : " + message.toString());
					sent = true;
				}
			}
		} 
		
		return StatusFactory.ok(this);
	}
	
}
